import regeneratorRuntime from '../utils/runtime';
import {
	Config,
	Version
} from "./config";
import {
	Util
} from "../utils/util";
const throttle = function(fn, gapTime) {
	if(gapTime == null || gapTime == undefined) {
		gapTime = 1500
	}
	let _lastTime = null
	return function() {
		let _nowTime = +new Date()
		if(_nowTime - _lastTime > gapTime || !_lastTime) {
			// 将this和参数传给原函数
			fn.apply(this, arguments)
			_lastTime = _nowTime
		}
	}
}
export const request = (url, data = {}, param = {
	header: {},
	dataType: 'json',
	responseType: 'text'
}, method = "GET") => {
	return new Promise((resolve, reject) => {
		let header = param.header || {};
		uni.showLoading();
		uni.request({
			url,
			data,
			header: param.header,
			method,
			success: res => {
				// 对返回请求错误的处理（非网络错误）
				// 更详细的暂时没写
				if(res.statusCode !== 200) {
					if(res.statusCode === 401) {} else {
						reject(res.data);
					}
				} else {
					if(res.data.code === 200) {
						resolve(res.data.data);
					} else {
						uni.showToast({
							icon: "none",
							title: res.data.msg
						});
						if(res.data.code === 401) {} else if(res.data.code === 403) {} else {
							reject(res.data);
						}
					}
				}
			},
			fail: reject,
			complete: () => {
				uni.hideLoading();
			}
		});
	});
}

export const uploadFile = (url, filePath, name = 'file', param = {
	header: {},
	formData: {}
}, uploadTask, index) => {
	return new Promise((resolve, reject) => {
		console.log(url, header)
		let uptask = uni.uploadFile({
			url,
			filePath,
			name,
			header: param.header || {},
			formData: param.formData || {},
			success: async(res) => {
				if(res.statusCode !== 200) {
					if(res.statusCode === 401) {} else {
						reject(res.data);
					}
				} else {
					let data = JSON.parse(res.data);
					resolve(data.body);
				}
			},
			fail: reject
		});
		if(uploadTask) {
			if(typeof uploadTask === 'function') {
				uploadTask(uptask, filePath, index);
			}
		}
	});
}

//网络接口类（所有网络请求写在这里面）
export class NetService {
	static Token = '';
	// 外链图片

	static CheckToken() {
		return new Promise((resolve, reject) => {
			if(wx.getStorageSync('Token')) {
				resolve(true);
			} else {
				resolve(false);
			}
		});
	}

	/**
	 * 微信登录接口
	 * @param {*} timeout 
	 */
	static Login(timeout = 3000) {
		return new Promise((resolve, reject) => {
			wx.login({
				timeout: timeout,
				success: resolve,
				fail: reject
			});
		});
	}

	/**
	 * 获取用户信息
	 * @param {*} timeout 
	 */
	static GetUserInfo(timeout = 3000) {
		return new Promise((resolve, reject) => {
			/* wx.getUserInfo({
				timeout: timeout,
				lang: 'zh_CN',
				success: resolve,
				fail: reject
			}); */
		});
	}

	/**
	 * 网络请求基础函数
	 * @param {string} Url 
	 * @param {object} Data 
	 * @param {object} Method 
	 * @param {boolean} IsApplyToken 
	 * @param {boolean} IsOtherArgs 额外的参数
	 */
	static async _Request(Url, Data, Method = "GET", IsType = 1) {
		try {
			let param = {
				header: {
					//"content-type": "application/json;charset=UTF-8"
				}
			}
			return await request(Url, Data, param, Method);
		} catch(error) {
			throw error;
		}
	}

	/**
	 * @description 通用文件上传
	 * @parmas {object} file 文件
	 * @parmas {string} Folder 自定义目录
	 */
	static async _WXUpLoadFile(url, file, uploadTask, index) {
		try {
			const param = {
				header: {
					// "content-type": "application/json",
					"Authorization": this.Token
				},
				formData: {}
			}
			return await uploadFile(url, file, 'file', param, uploadTask, index);
		} catch(error) {
			throw error;
		}
	}

	/**********************************************************************/

	/**
	 * @description 首页
	 * @param {Object} data 请求数据
	 */
	static async GetCode(data) {
		return await this._Request(Config.GetCode.url + "?code=" + data, {}, Config.GetCode.method, Config.GetCode.type);
	}
	static async wxgetuser(data) {
		return await this._Request(Config.wxgetuser.url + "?openid=" + data, {}, Config.wxgetuser.method, Config.wxgetuser.type);
	}
	static async wxsaveuser(data) {
		return await this._Request(Config.wxsaveuser.url, data, Config.wxsaveuser.method, Config.wxsaveuser.type);
	}
	static async getcatepidtree(data) {
		return await this._Request(Config.getcatepidtree.url, data, Config.getcatepidtree.method, Config.getcatepidtree.type);
	}
	static async wxConfig(data) {
		return await this._Request(Config.wxConfig.url + "?url=" + data, {}, Config.wxConfig.method, Config.wxConfig.type);
	}
	static async recWxsaverec(data) {
		return await this._Request(Config.recWxsaverec.url, data, Config.recWxsaverec.method, Config.recWxsaverec.type);
	}
	static async wxgetreclist(data) {
		return await this._Request(Config.wxgetreclist.url, data, Config.wxgetreclist.method, Config.wxgetreclist.type);
	}
	static async wxgetrecbyid(data) {
		return await this._Request(Config.wxgetrecbyid.url + "?id=" + data, {}, Config.wxgetrecbyid.method, Config.wxgetrecbyid.type);
	}
	static async wxrecact(data) {
		return await this._Request(Config.wxrecact.url + "?recID=" + data, {}, Config.wxrecact.method, Config.wxrecact.type);
	}
	static async wxtransit(data) {
		return await this._Request(Config.wxtransit.url, data, Config.wxtransit.method, Config.wxtransit.type);
	}
	static async wxacttopart(data) {
		return await this._Request(Config.wxacttopart.url + "?actDefID=" + data, {}, Config.wxacttopart.method, Config.wxacttopart.type);
	}
	static async wxgetuserbyusername(data) {
		return await this._Request(Config.wxgetuserbyusername.url, data, Config.wxgetuserbyusername.method, Config.wxgetuserbyusername.type);
	}
	static async wxassigntask(data) {
		return await this._Request(Config.wxassigntask.url, data, Config.wxassigntask.method, Config.wxassigntask.type);
	}
	static async wxcheck(data) {
		return await this._Request(Config.wxcheck.url, data, Config.wxcheck.method, Config.wxcheck.type);
	}
	static async wxprocessed(data) {
		return await this._Request(Config.wxprocessed.url, data, Config.wxprocessed.method, Config.wxprocessed.type);
	}
	static async sendVerifyCode(data) {
		return await this._Request(Config.sendVerifyCode.url, data, Config.sendVerifyCode.method, Config.sendVerifyCode.type);
	}
	static async codeVerify(data) {
		return await this._Request(Config.codeVerify.url, data, Config.codeVerify.method, Config.codeVerify.type);
	}
	static async sendMessage(data) {
		return await this._Request(Config.sendMessage.url, data, Config.sendMessage.method, Config.sendMessage.type);
	}
	/*	static async wxtransit(data) {
			return await this._Request(Config.wxtransit.url, data, Config.wxtransit.method, Config.wxtransit
				.type);
		}*/
}